//
//  AiCustomDao.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/10/31.
//

import Foundation
import GRDB

//自定义错误信息
class AiCustomErrorDao {
    func getList(id:String, pageNo: Int, pageSize: Int) async throws -> [AiCustomErrorBean] {
        return try await DatabaseRecordManager.shared.dbQueue.read { db in
            try AiCustomErrorBean
                .filter(Column("id") == id)
                .order(Column("time").desc)
                .limit(pageSize, offset: pageNo * pageSize)
                .fetchAll(db)
        }
    }
    
    
    func doSave(bean: AiCustomErrorBean) async throws {
        try await DatabaseRecordManager.shared.dbQueue.write { db in
            if (bean.objectId ?? "").isEmpty == true {
                bean.objectId = UUID().uuidString
                try bean.insert(db)
            } else {
                try bean.update(db)
            }
        }
    }
    
    func insert(bean: AiCustomErrorBean) async throws {
        try await DatabaseRecordManager.shared.dbQueue.write { db in
            try bean.insert(db)
        }
    }
    
    func doDel(bean: AiCustomErrorBean) async throws {
        _ = try await DatabaseRecordManager.shared.dbQueue.write { db in
            try AiCustomErrorBean
                .filter(Column("objectId") == bean.objectId)
                .deleteAll(db)
        }
    }
}
